//app.js
App({

  onLaunch: function () {
    var _that = this;

    var userToken = wx.getStorageSync('user_token');

    var userInfo = wx.getStorageSync('user_info');

    var isChecked = wx.getStorageSync('user_is_checked');

    var members = '';

    var stages = wx.getStorageSync('stages');

    _that.globalData.userInfo = userInfo;

    _that.globalData.isChecked = isChecked;

    // console.log(2,userToken);

    wx.showLoading({
      title: '加载中...'
    });

    if (!stages){

      wx.request({
        url: _that.globalData.requestUrl + 'api/stage/lists',
        method: 'POST',
        data: {
          user_token: _that.globalData.userToken,
        },
        success: function (res) {

          wx.setStorageSync('stages',res.data.data.lists);

        },
        fail: function (e) {
          wx.showLoading({
            title: '系统错误请稍后重试',
          });
        }
      })

    }
    if (!members) {

      wx.request({
        url: _that.globalData.requestUrl + 'api/user/lists',
        method: 'POST',
        data: {
          user_token: _that.globalData.userToken,
        },
        success: function (ret) {
          let data = ret.data;
          //根据分类ID查询分类名称：
          var lists = data.data.lists;
          for (var i in lists) {
            var majorName = lists[i][0].major_info.major_name;
            lists[majorName] = lists[i];
            delete lists[i];
          };

          let linea_member = [];
          //全部成员转为二维
          for (let key of Object.keys(lists)) {
            for (let index of lists[key]) {
              linea_member.push(index);

            }
          }
          wx.setStorageSync('members', linea_member);

        },
        fail: function (e) {
          wx.showLoading({
            title: '系统错误请稍后重试',
          });
        }
      })

    }



  },
  getToken: function (tiaozhuan){


    var _that = this;

    var userToken = wx.getStorageSync('user_token');

    var userInfo = wx.getStorageSync('user_info');

    var isChecked = wx.getStorageSync('user_is_checked');

    var members = '';

    var stages = wx.getStorageSync('stages');


    _that.globalData.userInfo = userInfo;

    _that.globalData.isChecked = isChecked;

    return new Promise(function (resolve, reject) {
      // 获取TOKEN
      if (userToken && userInfo) {

        _that.globalData.userToken = userToken;
        // console.log(userToken, 2, userInfo);
        resolve({
          status: 200
        });
      } else {
        wx.getUserInfo({
          success: function (res) {

            var userInfo = res.userInfo;

            var nickName = userInfo.nickName;
            var avatarUrl = userInfo.avatarUrl;
            var gender = userInfo.gender; //性别 0：未知、1：男、2：女
            var province = userInfo.province;
            var city = userInfo.city;
            var country = userInfo.country;

            if(userToken){

              wx.request({
                url: _that.globalData.requestUrl + 'api/get/userinfo',
                method: 'POST',
                data: {
                  user_token: userToken
                },
                success: function (res) {
                  if (res.data.status != 1) {
                    wx.showToast({
                      title: res.data.msg
                    });

                    resolve({
                      status: 200
                    });
                  } else {
                    var resInfo = res.data.data.info;

                    var _userInfo = null,
                        _isChecked = false;
                    if (resInfo) {
                      _userInfo = resInfo;
                      if (_userInfo.status == 1) {
                        _isChecked = true;
                      }
                    }

                    wx.setStorageSync('user_info', _userInfo);

                    wx.setStorageSync('user_is_checked', _isChecked);

                    _that.globalData.isChecked = _isChecked;
                    _that.globalData.userInfo = _userInfo;

                    if (tiaozhuan && _isChecked) {

                      wx.switchTab({
                        'url': '/pages/index/index'
                      });
                    }

                    if (!_isChecked && !tiaozhuan) {

                      wx.redirectTo({
                        'url': '/pages/sign/up'
                      });
                    } else {


                      resolve({
                        status: 200
                      });
                    }
                  }
                }
              });
            }else{

              wx.login({
                success: function (res) {
                  if (res.code) {
                    //发起网络请求
                    wx.request({
                      url: _that.globalData.requestUrl + 'api/login',
                      method: 'POST',
                      data: {
                        code: res.code,
                        nickname: nickName,
                        head_img: avatarUrl
                      },
                      success: function (res) {
                        if (res.data.status != 1) {
                          wx.showToast({
                            title: res.data.msg
                          })
                        } else {

                          // 成功
                          wx.setStorageSync('user_token', res.data.data.token);

                          _that.globalData.userToken = res.data.data.token;

                          userToken = res.data.data.token;

                          wx.request({
                            url: _that.globalData.requestUrl + 'api/get/userinfo',
                            method: 'POST',
                            data: {
                              user_token: userToken
                            },
                            success: function (res) {
                              if (res.data.status != 1) {
                                wx.showToast({
                                  title: res.data.msg
                                })
                              } else {
                                var resInfo = res.data.data.info;

                                var _userInfo = null,
                                    _isChecked = false;
                                if (resInfo) {
                                  _userInfo = resInfo;
                                  if (_userInfo.status == 1) {
                                    _isChecked = true;
                                  }
                                }

                                wx.setStorageSync('user_info', _userInfo);

                                wx.setStorageSync('user_is_checked', _isChecked);

                                _that.globalData.isChecked = _isChecked;
                                _that.globalData.userInfo = _userInfo;

                                if (tiaozhuan && _isChecked) {

                                  wx.switchTab({
                                    'url': '/pages/index/index'
                                  });
                                }

                                if (!_isChecked && !tiaozhuan) {

                                  wx.redirectTo({
                                    'url': '/pages/sign/up'
                                  });
                                } else {


                                  resolve({
                                    status: 200
                                  });
                                }
                              }
                            }
                          });

                          var member = res.data.data.member;

                          if (!tiaozhuan && (!member || member.status != 1)) {
                            wx.redirectTo({
                              'url': '/pages/sign/up'
                            });
                          }
                        }
                      }
                    })
                  } else {
                    console.log('登录失败！' + res.errMsg)
                    resolve({
                      status: 200
                    });
                  }
                }
              });
            }
            var encryptedData = res.encryptedData;

          }
        })
      }
    });

  },


  //getUserInfo: function (){
  //
  //  //app.getToken().then(function (){
  //  var userToken = wx.getStorageSync('user_token');
  //  let _isChecked = false;
  //  var _that = this;
  //
  //  return new Promise(function (resolve, reject) {
  //  })
  //},


  //getUserInfo: function (cb) {
  //  var that = this
  //  if (this.globalData.userInfo) {
  //    typeof cb == "function" && cb(this.globalData.userInfo)
  //  } else {
  //    //调用登录接口
  //    wx.login({
  //      success: function () {
  //        wx.getUserInfo({
  //          success: function (res) {
  //            that.globalData.userInfo = res.userInfo
  //            typeof cb == "function" && cb(that.globalData.userInfo)
  //          }
  //        })
  //      }
  //    })
  //  }
  //},
  globalData: {
    // 所有的request都用此域名
    requestUrl: 'http://pmapi.mqman.com/',
    userInfo: null,
    userToken: null,
    isChecked: false,
    members: null,
    requestOk: 0
  },
  addProject:function(){
    wx.navigateTo({
      url: '/pages/componet/addProject/addProject',
    })
  },
  removeValue: function (array, value) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == value) {
        array.splice(i, 1);
      }
    }
    return array;
  },
  //拨打电话全局方法，在<view>上设置：dataset-reply-phone = [[电话号码]]
  phoneCall: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.replyPhone,
      success: function () {
        console.log("成功拨打电话")
      }

    })

  }
})